<template>
  <div>
    <basic-container>
      <el-form ref="searchForm"
               :model="searchForm"
               size="mini"
               :inline="true">
        <el-form-item label="审批编码:" label-width="80px">
          <el-input v-model="searchForm.coded" placeholder="请输入审批编码"></el-input>
        </el-form-item>
        <el-form-item label="审批状态">
          <el-select v-model="searchForm.state"
                     placeholder="请选择">
            <el-option label="全部"
                       :value="-1"></el-option>
            <el-option label="未提交"
                       :value="0"></el-option>
            <el-option label="已提交"
                       :value="1"></el-option>
            <el-option label="审核中"
                       :value="2"></el-option>
            <el-option label="通过"
                       :value="4"></el-option>
            <el-option label="不通过"
                       :value="3"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="供应商">
          <el-select v-model="searchForm.supplierType"
                     supplierType
                     placeholder="请选择">
            <el-option label="全部"
                       :value="-1"></el-option>
            <el-option v-for="item in supplierType"
                       :key="item.id"
                       :value='item.id'
                       :label='item.name'></el-option>
          </el-select>
        </el-form-item>
       <!-- <el-form-item label="申请时间:">
          <el-date-picker v-model="searchForm.purchasingDate"
                          type="daterange"
                          range-separator="至"
                          start-placeholder="开始日期"
                          end-placeholder="结束日期">
          </el-date-picker>
        </el-form-item>-->
        <el-form-item label="">
          <el-button type="primary"
                     size="small"
                     plain
                     @click.stop="searchFormSubmit()">搜索</el-button>
          <el-button size="small"
                     plain
                     @click.stop="searchFormReset()">重置</el-button>
        </el-form-item>
      </el-form>
      <avue-crud :option="tabOption"
                 :data="tebData"
                 :table-loading="tebLoading"
                 :page="tebDataPage"
                 ref="crud"
                 @size-change='sizeChange'
                 @current-change="currentPage">
        <template slot-scope="scope"
                  slot="state">
          <span v-if="scope.row.state == 0">未提交</span>
          <span v-else-if="scope.row.state == 1">已提交</span>
          <span v-else-if="scope.row.state == 2">审核中</span>
          <span v-else-if="scope.row.state == 3">不通过</span>
          <span v-else-if="scope.row.state == 4">通过</span>
          <!-- {{scope.row.state}} -->
          <el-button type="info"
                     plain
                     size="mini"
                     @click.stop="processBtn(scope.row)">流程</el-button>
        </template>
        <template slot-scope="scope"
                  slot="menuLeft">
          <el-button type="primary"
                     size="small"
                     plain
                     @click.stop="addModeClick()">添加</el-button>
        </template>
        <template slot-scope="scope"
                  slot="menu">
          <el-button type="primary"
                     size="small"
                     plain
                     v-if="scope.row.state == 1 || scope.row.state== 2"
                     @click.stop="examineMenuBtn(scope.row,scope.index)">审核</el-button>
          <el-button type="primary"
                     size="small"
                     plain
                     @click.stop="seeMenuBtn(scope.row,scope.index)">查看</el-button>
          <el-button type="primary"
                     size="small"
                     plain
                     v-if="scope.row.state == 0"
                     @click.stop="revokeMenuBtn(scope.row,scope.index)">撤销</el-button>
          <el-button type="primary"
                     size="small"
                     plain
                     v-if="scope.row.state == 0"
                     @click.stop="delMenuBtn(scope.row,scope.index)">删除</el-button>
          <el-button type="primary"
                     size="small"
                     plain
                     v-if="scope.row.state == 0"
                     @click.stop="updateMenuBtn(scope.row,scope.index)">修改</el-button>
          <!-- <el-button type="primary"
                     size="small"
                     plain
                     @click.stop="exportMenuBtn(scope.row,scope.index)">导出</el-button> -->
        </template>
      </avue-crud>
    </basic-container>
    <!-- 流程 -->
    <el-dialog title="流程"
               :visible.sync="processMode"
               width="35%">
      <div class="block">
        <ul class="el-timeline is-reverse">
          <li class="el-timeline-item"
              v-for="item in processModeData"
              :key="item.id">
            <div class="el-timeline-item__tail"></div>
            <div class="el-timeline-item__node el-timeline-item__node--normal el-timeline-item__node--"></div>
            <div class="el-timeline-item__wrapper">
              <el-card>
                <div class="el-timeline-item__content">
                  <div>
                    审批人：{{item.purchasingDealerName}} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                    <span v-if="item.dealType == 0">未提交</span>
                    <span v-else-if="item.dealType == 1">已提交</span>
                    <span v-else-if="item.dealType == 2">通过</span>
                    <span v-else-if="item.dealType == 3">不通过</span>
                    <span v-else-if="item.dealType == 4">已撤销</span>
                  </div>
                  <div>{{item.dealDate}}</div>
                </div>
                <!-- <div class="el-timeline-item__timestamp is-bottom">{{item.dealDate}}</div> -->
                <h4>{{validatenull(item.dealNote)?"(无审批备注)":item.dealNote}}</h4>
              </el-card>
            </div>
          </li>
        </ul>
      </div>
    </el-dialog>
    <!-- 审核 -->
    <el-dialog title="审核"
               :visible.sync="examineModeState.mode"
               width="30%"
               center
               class="examineModeClass">
      <el-form ref="addModeForm"
               :model="examineModeState.form"
               :rules="addModeFormRules"
               label-width="80px"
               size="mini">
        <el-form-item label="备  注:"
                      prop="remarks">
          <el-input type="textarea"
                    maxlength="100"
                    :autosize="{ minRows: 4, maxRows: 10}"
                    v-model="examineModeState.form.remarks"
                    placeholder="最多可输入100字"></el-input>
        </el-form-item>
      </el-form>

      <span slot="footer"
            class="dialog-footer"
            style="text-align: center;">
        <el-button size="small"
                   @click="examineModeState.mode = false">关 闭</el-button>
        <el-button size="small"
                   type="primary"
                   @click="submitExamineModeBtn">{{examineModeState.name}}</el-button>
      </span>
    </el-dialog>

    <!-- 添加采购单 -->
    <el-dialog title="采购单" :visible.sync="addMode"  width="70%" center>
      <el-form ref="addModeForm" :model="addModeForm"  :rules="addModeFormRules"  label-width="70px"  size="mini">
        <el-col :span="6">
          <el-form-item label="供应商" prop="supplierType">
            <el-select v-model="addModeForm.supplierType"  supplierType placeholder="请选择">
              <el-option v-for="item in supplierType"  :key="item.id"  :value='item.id'  :label='item.name'></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="18">
          <el-form-item label="备  注:">
            <el-input type="textarea"  maxlength="50"  :autosize="{ minRows: 1, maxRows: 1}"
                      v-model="addModeForm.remarks" placeholder="最多可输入50字"></el-input>
          </el-form-item>
        </el-col>
      </el-form>
      <avue-crud class="customForm" :option="addModeTabOption" :data="addModeTabData" :table-loading="addModeTabLoading"
                  ref="addModeTab" @selection-change="addModeSelectionChange">
        <template slot-scope="scope"
                  slot="num">
          <el-input v-model.number="scope.row.num"
                    size='mini'
                    type="number"
                    :min="0" @change="inputNum(scope)"
                    placeholder="请输入"></el-input>
        </template>
        <template slot-scope="scope"
                  slot="price">
          <el-input v-model.number="scope.row.price"
                    size='mini'
                    :min="0" @change="inputNum(scope)"
                    type="number"
                    placeholder="请输入"></el-input>
        </template>
        <template slot-scope="scope"
                  slot="totalPrice">
          {{scope.row.totalPrice}}
        </template>
        <template slot-scope="scope"
                  slot="menuLeft">
          <el-button type="primary"
                     size="small"
                     plain
                     @click.stop="addModeSelectModeClick()">选择物品</el-button>
          <el-button type="primary"
                     size="small"
                     plain
                     @click.stop="addModeDelMenuBtn(scope.row,index)">删除</el-button>
          <el-form size="mini">
            <el-form-item>
              <el-input v-model='barcode' @keyup.enter.native="barCodeSearch()" placeholder="使用条形码添加商品"></el-input>
            </el-form-item>
          </el-form>
          <!-- <el-button type="primary"
                     size="small"
                     plain
                     @click.stop="addModeSelectMode = true">从预预警存选择</el-button> -->
        </template>
      </avue-crud>

      <span slot="footer"
            class="dialog-footer">
        <el-button size="small"
                   @click="addMode = false">关 闭</el-button>
        <el-button size="small"
                   @click="addModeSave()">保 存</el-button>
        <el-button size="small"
                   type="primary"
                   @click="addModeUp()">提 交</el-button>
      </span>
    </el-dialog>

    <!-- 添加选择物品 -->
    <el-dialog title="选择物品"
               :visible.sync="addModeSelectMode"
               width="60%"
               center
               class="addModeClass">
      <el-col :span="24">
        <el-form ref="tableOptionSearch"
                 :model="addModeSelectModeForm"
                 :inline="true"
                 size="mini">
          <!-- <el-col :span="6">
            <el-form-item label="仓库:"
                          label-width="50px">
              <el-select v-model="addModeSelectModeForm.state"
                         placeholder="请选择">
                <el-option v-for="item in warehouseList.list"
                           :key="item.id"
                           :label="item.name"
                           :value="item.id"></el-option>

              </el-select>
            </el-form-item>
          </el-col> -->
          <el-form-item label="物品类型:"
                        label-width="80px">
            <el-cascader v-model="addModeSelectModeForm.type"
                         :options="dangerousTypeAllS"
                         :show-all-levels="false"></el-cascader>
          </el-form-item>
          <el-form-item label="物品编码:"
                        label-width="80px">
            <el-input v-model="addModeSelectModeForm.coded"
                      placeholder="请输入物品编码"></el-input>
          </el-form-item>
          <el-form-item label="物品名称:"
                        label-width="80px">
            <el-input v-model="addModeSelectModeForm.name"
                      placeholder="请输入物品名称"></el-input>
          </el-form-item>
          <el-form-item label-width="20px">
            <el-button type="primary"
                       @click="addModeSelectModeSearch()">搜索</el-button>
          </el-form-item>
        </el-form>
      </el-col>

      <el-col :span="24">
        <!-- <el-col :span="4">
          &lt;!&ndash; <p class="type-class"
             @click="dangerousTypeItemClick(item.id)"
             v-for="item in dangerousTypeAll"
             :key="item.id">{{item.name}}</p> &ndash;&gt;
          <el-tree :data="dangerousTypeAll"
                   :props="defaultProps"
                   @node-click="dangerousTypeAllClick"></el-tree>
        </el-col>
        <el-col :span="20">-->
        <avue-crud :option="addModeSelectModeTabOption"
                   :data="addModeSelectModeTabData"
                   :table-loading="addModeSelectModeTabLoading"
                   :page="addModeSelectModeTabpage"
                   @selection-change="addModeSelectModeTabChange">
          <template slot-scope="scope"
                    slot="dangerousList">
            <span v-if="scope.row.dangerousList">{{scope.row.dangerousList.overdueNum}}</span>
          </template>
        </avue-crud>
      </el-col>

      <!-- </el-col>-->

      <span slot="footer"
            class="dialog-footer">
        <el-button size="small "
                   @click="addModeSelectMode = false">取 消</el-button>
        <el-button size="small "
                   type="primary"
                   @click="addGoodsClick()">添 加</el-button>
      </span>
    </el-dialog>

    <!-- 采购单 -->
    <el-dialog title="采购单"
               :visible.sync="examineMode"
               width="60%"
               center
               class="examineModeClass">
      <el-row>
        <el-col :span="24">审批单号：{{examineModeTabData.coded}}</el-col>
      </el-row>
      <el-row>
        <el-col :span="6">申请人：{{examineModeTabData.applyUserInfoName}}</el-col>
        <el-col :span="6">申请时间：{{examineModeTabData.purchasingDate}}</el-col>
      </el-row>
      <el-row>
        <el-col :span="24">备注：{{examineModeTabData.note}}</el-col>
      </el-row>

      <avue-crud class="examineCustomForm"
                 :option="examineModeTabOption"
                 :data="examineModeTabData.purchasingList"
                 :table-loading="examineModeTabLoading"
                 :page="examineModeDataPage">
        <template slot-scope="scope"
                  slot="menuLeft">
          供应商：{{examineModeTabData.supplierName}}
        </template>
        <template slot-scope="scope"
                  slot="menuRight">
          <span v-show="examineModeType == 1">总金额：{{allPrice}} 元</span>
          <!-- <span v-show="examineModeType == 2">含税总金额：000000000.00 元</span> -->
        </template>
      </avue-crud>

      <!--<el-row v-show="examineModeType == 2">
        <el-col :span="6">审批状态：
          <span v-if="examineModeTabData.state==0">未提交</span>
          <span v-else-if="examineModeTabData.state==1">审核中</span>
          <span v-else-if="examineModeTabData.state==2">不通过</span>
          <span v-else-if="examineModeTabData.state==3">通过</span>
        </el-col>
        <el-col :span="6">审 批 人：{{examineModeTabData.dealUserName}}</el-col>
        <el-col :span="6">审批时间：{{examineModeTabData.dealDate}}</el-col>
      </el-row>
      <el-row v-show="examineModeType == 2">
        <el-col :span="24">备注：{{examineModeTabData.note}}</el-col>
      </el-row>-->

      <span slot="footer"
            class="dialog-footer"
            style="text-align: center;">
        <el-button v-show="examineModeType == 1"
                   size="small"
                   @click="adopt(false)">不通过</el-button>
        <el-button v-show="examineModeType == 1"
                   size="small"
                   type="primary"
                   @click="adopt(true)">通 过</el-button>

        <el-button v-show="examineModeType == 2 && examineModeTypeTj == 0"
                  size="small"
                   type="primary"
                   @click="addModeUp()">提 交</el-button>
        <!-- <el-button v-show="examineModeType == 2"
                   size="small"
                   type="primary"
                   @click="examineMode = false">打印</el-button> -->
        <el-button size="small"
                   @click="examineMode = false">关 闭</el-button>
      </span>
    </el-dialog>

  </div>
</template>

<script>
import { mapState, mapActions } from 'vuex'
import { tabOption, addModeTabOption, addModeSelectModeTabOption, examineModeTabOption } from "@/const-personal/dangerous/consumablesMining/purchase.js"
import { validatenull } from '@/util/validate'
import { getGoodsTypeInfo} from "@/api/dangerous/consumablesMining/purchase"
import { getListWarehouseAll } from "@/api/public/warehouse.js"
import * as http from "@/api/consumables/consumablesMining/purchase"
import excel from '@/util/excel'
import { tableOptionModeAddFormRulesGYS } from "@/const-personal/equipment/appointment/lend.js"
import { DepartmentTreeVChildrenNull } from '@/util/util'
import { loginByUsername } from "../../../api/user";
import { getSupplierAll } from "@/api/sysSetup/supplierManage";
export default {
  data () {
    return {
      barcode:'',
      barcodeType:false,
      dangerousGoodsData: [],
      warehouseData: [],
      defaultProps: {
        children: 'children',
        label: 'label'
      },
      examineModeState: {
        mode: false,
        form: {
          remarks: ''
        }
      }, // 处理审核
      addModeFormRules: tableOptionModeAddFormRulesGYS,
      //表格结构
      tabOption: tabOption,
      addModeTabOption: addModeTabOption,
      addModeSelectModeTabOption: addModeSelectModeTabOption,
      examineModeTabOption: examineModeTabOption,
      //表格数据
      tebData: [],
      addModeTabData: [{
          totalPrice:0,
          num:0,
          price:0,
      }],
      examineModeTabData: {
        purchasingList: []
      },
      examineModeTypeTj:0,
      addModeSelectModeTabData: [],
      //表格等待
      tebLoading: false,
      addModeTabLoading: false,
      addModeSelectModeTabLoading: false,
      examineModeTabLoading: false,
      //表格分页
      tebDataPage: {
        total: 0, //总页数
        currentPage: 1, //当前页数
        pageSize: 10, //每页显示多少条
        pageSizes: [5,10,20] //显示多少条数据
      },
      addModeTabDataPage: {
        total: 0, //总页数
        currentPage: 1, //当前页数
        pageSize: 10, //每页显示多少条
        pageSizes: [5,10,20] //显示多少条数据
      },
      addModeSelectModeTabpage: {
        total: 0, //总页数
        currentPage: 1, //当前页数
        pageSize: 10, //每页显示多少条
        pageSizes: [5,10,20] //显示多少条数据
      },
      examineModeDataPage: {
        total: 0, //总页数
        currentPage: 1, //当前页数
        pageSize: 10, //每页显示多少条
        pageSizes: [5,10,20] //显示多少条数据
      },
      //搜索表单数据
      searchForm: {
        state: -1, //状态
        purchasingDate: '', //申请时间
        supplierType: -1,//供应商类型
      },
      //添加表单数据
      addModeForm: {

      },
      //添加模态框选中数据
      addModeSelectionList: [],
      //添加表单筛选设别
      addModeSelectModeForm: {
        coded: '',
        name: '',
        type: ['-1']
      },
      //添加表单验证
      // addModeFormRules: {},
      //流程模态框
      processMode: false,
      processModeData: {},
      //添加模态框
      addMode: false,
      examineMode: false,
      //添加选择设别
      addModeSelectMode: false,
      //采购单模态框状态
      examineModeType: 0,
      //流程数据
      activities: [],
      //树状图数据
      treeData: [],
      // order: [],
      dangerousTypeAll: [],//所以危险品类型
        updataId:null,
        dangerousTypeAllS:[]
    }
  },
  created () {
    this.getWarehouseData();
    // this.searchFormSubmit();
    this._getPurcharse() //采购管理列表数据
    // this._getSupplierType() //供应商类型
    this._getWareList()
    this._getSupplierAll()
  },
  computed: {
    ...mapState({
      supplierType: state => state.equipmentLedger.supplierType, //供应商类型
      warehouseList: state => state.equipmentLedger.warehouseList, //仓库列表数据
    }),
    //查看总金额
    allPrice () {
      let price = 0, arr = this.examineModeTabData.purchasingList
      Array.isArray(arr) && arr.forEach((item) => {
        // console.log(price);
        price += item.totalPrice
      })
      return price
    },
  },
  watch:{
      barcode: function (newQuestion, oldQuestion) {
          if(this.barcodeType){
              this.barcodeType = false
              this.barcode = newQuestion.substr(oldQuestion.length,newQuestion.length)
          }
      }
  },
  methods: {
      inputNum(scope){
          let data = JSON.parse(JSON.stringify(this.addModeTabData));
          data[scope.row.$index].totalPrice = (scope.row.num * scope.row.price).toFixed(2)
          this.addModeTabData = data
      },
    ...mapActions('equipmentLedger', ['_getSupplierType', '_getWarehouseList','_getSupplierAll']),
    //采购管理列表数据
    _getPurcharse (search) {
      let params = {
        page: this.tebDataPage.currentPage,
        size: this.tebDataPage.pageSize
      }
      if (search) {
        params['coded'] = search.coded
        params['state'] = search.state == -1 ? null : search.state;
        params['supplierId'] = search.supplierType == -1 ? null : search.supplierType;
        params['startTime'] = Array.isArray(search.purchasingDate) ? this.$moment(search.purchasingDate[0]).format('YYYY-MM-DD') : ''
        params['endTime'] = Array.isArray(search.purchasingDate) ? this.$moment(search.purchasingDate[1]).format('YYYY-MM-DD') : ''
      }
      http.getDangerousPurchasing(params).then(res => {
        if (res.data.code === 200) {
          this.tebData = res.data.data.list
          this.tebDataPage.total = res.data.data.total;
          this.tebDataPage.currentPage = res.data.data.pageNum;
          this.tebDataPage.pageSize = res.data.data.pageSize;
        } else {
          this.$notify.error({ title: '查询失败', message: '获取数据失败！', type: 'danger', });
        }
      })
    },
    //仓库列表请求
    _getWareList () {
      let params = {
        page: this.addModeTabDataPage.currentPage,
        size: this.addModeTabDataPage.pageSize
      }
      this._getWarehouseList(params)
    },
    //流程
    processBtn (row, index) {
      // this.processModeData = row
      http.dangerousPurchasingState({ id: row.id }).then(res => {
        if (res.data.code === 200) {
          this.processModeData = res.data.data
          this.processMode = true;
        } else {
          this.$notify.error(res.data.message)
        }
      })
    },
    //仓库数据
    getWarehouseData () {
      getListWarehouseAll().then(res => {
        if (res.data.code == 200) {
          this.warehouseData = res.data.data
        } else {
          this.$notify.error({ title: '查询失败', message: '获取仓库数据失败！', type: 'danger', });
        }
      })
    },
    //添加采购单
    addModeClick () {
      // console.log(this.warehouseList);
      this.addModeTabData = []
      // this.addModeTabDataPage.total = this.warehouseList.total
      this.addMode = true
      this.update_id = null
      this.updataId = null

    },
    //修改采购单
    updateMenuBtn (row) {
      this.update_id = row.id
      this.updataId = row.id
      this._httpDangerousFindById(row.id, 'update')
      this.addMode = true
    },
    //添加物品
    addGoodsClick () {
      //采购单数据
      this.checkAddWp.forEach(item => {
        let flog = true;
        this.addModeTabData.forEach(itmes => {
          if (item.id == itmes.id) {
            flog = false;
          }
        })
        if (flog) {
          item['kcNum'] = item.inventoryCount
          item.num = 0
          item.price = 0
          item.totalPrice = '0.00'
          item.goodsTypeInfoId = item.id,
            console.log(item);
          this.addModeTabData.push(item)
        }
      })
      this.addModeSelectMode = false
    },
    barCodeSearch(){
        let params = {
            barcode:this.barcode,
            page: 1,
            size: 10,
        }
        getGoodsTypeInfo(params).then(res => {
            if(res.data.code == 200 && res.data.data.list.length > 0) {
                res.data.data.list.forEach(item=>{
                    let flog = true;
                    if (flog) {
                        item['kcNum'] = item.inventoryCount
                        item.num = 0
                        item.price = 0
                        item.totalPrice = '0.00'
                        item.goodsTypeInfoId = item.id,
                        this.addModeTabData.push(item)
                    }
                })
            } else {
                this.$notify.error({ title: '查询失败', message: '该物品不存在！', type: 'danger', });
            }
            this.barcodeType = true
        })
    },
    //采购单保存
    addModeSave () {
      // console.log(this.addModeTabData);
      if (this.addModeForm.supplierType) {
        if (this.update_id) {
          let paramsArr = this.addModeTabData.map(item => {
              return {
                  goodsTypeInfoId: item.goodsTypeInfoId,
                  num: item.num,
                  price: item.price,
              }
          })
          let params = {
              purchasingList: paramsArr,
              supplierId: this.addModeForm.supplierType,
              note: this.addModeForm.remarks
          }
          params.purchasingList.forEach(item=>{
              item.goodsTypeInfoId
          })
          params['id'] = this.update_id
          http.updateDangerous(params).then(res => {
              if (res.data.code === 200) {
                  this.isAddGoods = true
                  this.isAddGoodsId = res.data.data.id
                  this.$notify({title: '操作成功', type: 'success', message: '修改成功,请提交!'});
                  this.searchFormSubmit()
              } else {
                  this.$notify.error({title: '操作失败', type: 'danger', message: res.data.message});
              }
          })
        } else {
            let paramsArr = this.addModeTabData.map(item => {
                return {
                    goodsTypeInfoId: item.id,
                    num: item.num,
                    price: item.price,
                    // goodsTypeInfoId:
                }
            })
            let params = {
                purchasingList: paramsArr,
                supplierId: this.addModeForm.supplierType,
                note: this.addModeForm.remarks
            }
            http.addGoods(params).then(res => {
                if (res.data.code === 200) {
                    this.isAddGoods = true
                    this.isAddGoodsId = res.data.data.id
                    this.$notify({title: '操作成功', type: 'success', message: '添加成功,请提交!'});
                    this.searchFormSubmit()
                } else {
                    this.$notify.error({title: '操作失败', type: 'danger', message: res.data.message});
                }
            })
        }
      } else {
          this.$notify.error({ title: '提示', type: 'danger', message:'请选择供应商'});
      }
    },
    //采购单提交
    addModeUp () {
      if (this.isAddGoods) {
        http.addGoodsUp({ id: validatenull(this.isAddGoodsId) ? this.updataId : this.isAddGoodsId }).then(res => {
          if (res.data.code === 200) {
            this._getPurcharse()
              this.$notify({ title: '操作成功', type: 'success', message: '提交成功!' });
              this.addMode = false
              this.searchFormSubmit()
          } else {
            this.$notify.error(res.data.message)
          }
        })
      } else {
          this.$notify.error({ title: '提示', type: 'danger', message:'请保存之后再提交'});
      }
    },
    //选择物品按钮
    addModeSelectModeClick () {
      //所以危险品类型
      http.getDangerousType().then(res => {
        if (res.data.code === 200) {
          this.dangerousTypeAll = DepartmentTreeVChildrenNull(res.data.data)
          this.dangerousTypeAllS = DepartmentTreeVChildrenNull(res.data.data,[{label:'全部',value:'-1'}])
          // console.log(this.dangerousTypeAll);
          this.dangerousTypeItemId = res.data.data[0].id
          this.dangerousTypeItemClick()
        } else {
          this.$notify.error(res.data.message)
        }
      })
      this.addModeSelectMode = true
    },
    //根据危险品类型id去查物品
    dangerousTypeItemClick (search) {
      let params = {
        dangerousTypeId: this.addModeSelectModeForm.type.length == 0 ? null : this.addModeSelectModeForm.type[this.addModeSelectModeForm.type.length - 1],
        page: this.addModeSelectModeTabpage.currentPage,
        size: this.addModeSelectModeTabpage.pageSize
      }
      params.dangerousTypeId = params.dangerousTypeId == '-1' ? null : params.dangerousTypeId;
      if (search) {
        params['page'] = 1
        params['name'] = search.name
        params['coded'] = search.coded
        params['coded'] = search.coded
      }
      http.getGoodsTypeInfo(params).then(res => {
        if (res.data.code === 200) {
          this.addModeSelectModeTabData = res.data.data.list
          this.addModeSelectModeTabpage.total = res.data.data.total
          this.addModeSelectModeTabpage.pageSize = res.data.data.pageSize
          this.addModeSelectModeTabpage.currentPage = res.data.data.pageNum
        } else {
          this.$notify.error(res.data.message)
        }
      })
    },
    //dangerousTypeAllClick tree
    dangerousTypeAllClick (data) {
      console.log(data);
      this.dangerousTypeItemId = data.id
      this.dangerousTypeItemClick()
    },
    //删除
    delMenuBtn (row, index) {
      this.$confirm('删除后将无法找回, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        //请求-删除
        // this.$delete(this.tebData, row.$index)
        http.deleteDangerous({ id: row.id }).then(res => {
          if (res.data.code === 200) {
            this._getPurcharse()
            this.$notify({ title: '删除成功', type: 'success', });
          } else {
            this.$notify({ title: res.data.message, type: 'error', });
          }
        })
        // this.$notify({ title: '删除成功', type: 'success', });
      }).catch(() => {
        this.$notify({ title: '已取消删除', type: 'info', });
      });
    },
    //审核
    examineMenuBtn (row, index) {
      // console.log(row);
      // this.examineModeTabData = row
      // console.log(this.examineModeTabData);

      this.examineMode = true;
      this.examineModeType = 1;
      this.adoptId = row.id
      this._httpDangerousFindById(row.id, 'shenhe')
    },
    //审核查看等公共请求
    _httpDangerousFindById (id, how) {
      http.dangerousFindById({ id }).then(res => {
        if (res.data.code === 200) {
          //修改
          if (how === 'update') {
            this.addModeForm.supplierType = res.data.data.supplierId
            this.addModeForm.remarks = res.data.data.note
            this.addModeTabData = []
              res.data.data.purchasingList.forEach(item => {
              // console.log(item, '-------')
                  item.kcNum = item.inventoryCount;
                  item.coded = item.goodsTypeInfo.coded;
                  item.unit = item.goodsTypeInfo.unit;
                  item.name = item.goodsTypeInfo.name;
                  item.specs = item.goodsTypeInfo.specs;
                  item.barcode = item.goodsTypeInfo.barcode;
                  this.addModeTabData.push(item)
            })
            console.log(this.addModeTabData);
          } else if (how === 'shenhe') { //审核，查看数据处理
            this.examineModeTabData = res.data.data
            this.examineModeDataPage.total = res.data.total
            this.examineModeDataPage.currentPage = res.data.pageNum
            this.examineModeDataPage.pageSize = res.data.pageSize
            this.examineModeTabData['purchasingList'] = this.examineModeTabData.purchasingList.map(item => {
              // console.log(item, '-------')
              return {
                ...item,
                ...item.goodsTypeInfo
              }
            })
          }
        } else {
          this.$notify.error(res.data.message)
        }
      })
    },
    //审核通过
    adopt (status) {
      this.examineModeState = {
        mode: true,
        name: status ? '通 过' : '不通过',
        id: this.adoptId,
        status: status ? 1 : 0,
        form: {
          remarks: ''
        }
      }
    },
    //执行通过 and 不通过请求
    submitExamineModeBtn () {
      let params = {
        id: this.examineModeState.id,
        status: this.examineModeState.status,
        remarks: this.examineModeState.form.remarks
      }
      // console.log(params);
      http.dangerousToExamine(params).then(res => {
        if (res.data.code === 200) {
          this._getPurcharse()
          this.$notify.success(res.data.message)
          this.examineModeState.mode = false
          this.examineMode = false
        } else {
          this.$notify.error(res.data.message)
        }
      })
    },
    //查看
    seeMenuBtn (row, index) {
      this.examineMode = true;
      this.examineModeType = 2;
      console.log(row.state)
      this.examineModeTypeTj = row.state;
      this.isAddGoods = true
        this.isAddGoodsId = row.id
      this._httpDangerousFindById(row.id, 'shenhe')
    },
    // //导出
    // exportMenuBtn (row, index) {
    //   let filterTabDatas = this.tebData;
    //   if (filterTabDatas.length > 0) {
    //     const params = {
    //       title: ['序号', '物品编号', '物品名称', '规格', '库存', '将过期/已过期库存', '采购数量', '单位', '单价(元)', '总金额(元)'],
    //       key: ['id', 'name', 'phone', 'date'],
    //       data: filterTabDatas,
    //       autoWidth: true,
    //       filename: '采购单物品'
    //     }
    //     excel.export_array_to_excel(params)
    //   } else {
    //     this.$message({
    //       type: 'info',
    //       message: '请选择要导出的数据'
    //     });
    //   }
    // },

    //撤销
    revokeMenuBtn (row, index) {
      this.$confirm('撤销后，申请单将变为未提交状态！', '提示', {
        confirmButtonText: '确定', cancelButtonText: '取消', type: 'warning'
      }).then(() => {
        //请求--撤销
        http.dangerousRevoke({ id: row.id }).then(res => {
          if (res.data.code === 200) {
            this._getPurcharse()
            this.$notify({ title: '撤销成功', type: 'success', });
          } else {
            this.$notify({ title: res.data.message, type: 'error', });
          }
        })
        // this.$notify({ title: '撤销成功', type: 'success', });
      });
    },
    //删除
    addModeDelMenuBtn (row) {
      if (validatenull(this.addModeSelectionList)) {
        this.$notify.error({ title: '操作失败', message: '请选择至少一条数据！', type: 'danger', });
        return;
      }
      this.$confirm('此操作将删除该数据, 是否继续?', '提示', {
        confirmButtonText: '确定', cancelButtonText: '取消', type: 'warning'
      }).then(() => {
        //请求--删除
        this.addModeSelectionList.reverse()
        this.addModeSelectionList.forEach(item => {
          this.$delete(this.addModeTabData, item.$index)
        })
        this.$refs['addModeTab'].toggleSelection(this.addModeTabselectionList);
        this.$notify({ title: '操作成功', type: 'success', message: '删除成功!'});
      });
    },
    addModeSelectionChange (list) {
      this.addModeSelectionList = list
    },
    //选择添加物品
    addModeSelectModeTabChange (list) {
      this.checkAddWp = list
    },
      // 切换页面
      currentPage (val) {
        this.tebDataPage.currentPage = val;
        this.searchFormSubmit();
      },
      sizeChange (val) {
        this.tebDataPage.pageSize = val
        this.searchFormSubmit()
      },
    //提交搜索
    searchFormSubmit () {
      this._getPurcharse(this.searchForm)
    },
    addModeSelectModeSearch () {
      this.dangerousTypeItemClick(this.addModeSelectModeForm)
      /*this.addModeSelectModeForm = {}*/
    },
    //清空搜索
    searchFormReset () {
      this.searchForm = {}
    }
  }
}
</script>

<style>
.customForm .avue-crud__menu {
  float: left;
  display: inline-block;
  width: auto;
}
.customForm .avue-crud__right {
  float: right;
}
.customForm .avue-crud__right button {
  margin-right: 0px !important;
  margin-left: 8px !important;
}
.customForm .avue-crud__tip {
  margin: 0px;
}
.customForm .avue-crud__left{
  display: -webkit-inline-box;
  margin-right: 10px;
}
.customForm .avue-crud__tip{
  display: none;
}
.addModeClass .avue-crud__tip {
  margin: 0px 0px 8px !important;
}
.addModeClass .avue-crud__menu {
  margin-bottom: 0px !important;
}
.examineModeClass .el-dialog__body {
  padding: 5px 25px 0px !important;
}
.examineCustomForm {
  width: 100% !important;
}
</style>
<style scoped>
.el-timeline-item__content {
  display: flex;
  justify-content: space-between;
}
</style>